﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Milkify.Interfaces;
using Milkify.Data;
using IronCow;

namespace Milkify.Controllers
{
    class TaskEditController : Controller, ITaskEditController
    {
        private ITaskEditView mView;

        public TaskEditController(ControllerContext context, Rtm rtm, IViewContainer viewContainer, IViewFactory viewFactory)
            : base(context, rtm, viewContainer)
        {
            mView = (ITaskEditView)viewFactory.Create("TaskEdit");
            mView.EditComplete += new EventHandler(View_EditComplete);
            mView.EditCancelled += new EventHandler(View_EditCancelled);
        }

        public override void Execute()
        {
            if (Context.CommandName == "AddTask")
            {
                Task newTask = new Task();
                newTask.Due = Properties.Settings.Default.DefaultDueDate;
                IEnumerable<TaskList> taskLists = Rtm.GetParentableTaskLists(true);
                IEnumerable<string> tags = Rtm.GetTags();
                IEnumerable<Location> locations = Rtm.Locations;
                TaskList suggestedTaskList = null;
                if (ViewContainer.CurrentView is ISuggestsTaskList)
                    suggestedTaskList = ((ISuggestsTaskList)ViewContainer.CurrentView).SuggestedTaskList;
                if (suggestedTaskList == null)
                    suggestedTaskList = Rtm.GetInbox();
                TaskCreateData editData = new TaskCreateData(newTask, suggestedTaskList, taskLists, tags, locations);

                mView.ViewData = editData;
                mView.FocusNameControl();

                ViewContainer.NavigateToView(mView);
            }
            else if (Context.CommandName.StartsWith("EditTasks"))
            {
                Task[] tasks = Context.CommandParameter as Task[];
                IEnumerable<TaskList> taskLists = Rtm.GetParentableTaskLists(false);
                IEnumerable<string> tags = Rtm.GetTags();
                IEnumerable<Location> locations = Rtm.Locations;
                CompositeTaskEditData editData = new CompositeTaskEditData(tasks, taskLists, tags, locations);

                mView.ViewData = editData;
            }

            if (Context.CommandName.StartsWith("EditTasks"))
            {
                switch (Context.CommandName.Substring("EditTasks".Length))
                {
                    case "Name":
                    default:
                        mView.FocusNameControl();
                        break;
                    case "DueDate":
                        mView.FocusDueDateControl();
                        break;
                    case "Repeat":
                        mView.FocusRepeatControl();
                        break;
                    case "Estimate":
                        mView.FocusEstimateControl();
                        break;
                    case "Tags":
                        mView.FocusTagsControl();
                        break;
                    case "Url":
                        mView.FocusUrlControl();
                        break;
                    case "Location":
                        mView.FocusLocationControl();
                        break;
                    case "Priority":
                        mView.FocusPriorityControl();
                        break;
                }

                ViewContainer.NavigateToView(mView);
            }
        }

        private void View_EditCancelled(object sender, EventArgs e)
        {
        }

        private void View_EditComplete(object sender, EventArgs e)
        {
            var viewData = (TaskEditData)mView.ViewData;
            viewData.Commit();

            ViewContainer.InvalidateRefreshableViews();
        }
    }
}
